package com.pan.concurrency.thread;

/**
 * <p>
 *  代码同步块：使关键性执行代码由并行编程串行
 *
 *  1. 因为run里面只有一行代码，并且是非double和非long类型的操作值，所以是原子性的。不需要synchronized关键字
 *
 *
 * </p>
 *
 * @author kaiji
 * @since 2020/1/25 11:32
 */
public class SynchronizedThread {

    // private static int tickets = 100;

    public static void main(String[] args) {
        Sale saleZh = new Sale("张三");
        Sale saleLi = new Sale("李四");
        // for (int i = 0; i < 10; i++) {
            Thread zhSan = new Thread(saleZh);
            Thread liSi = new Thread(saleLi);

            zhSan.start();
            liSi.start();
        // }


    }



}

class Sale implements Runnable {
    private String username;
    private static int tickets = 100;

    Sale(String username) {
        this.username = username;
    }
    @Override
    public void run() {

        while (tickets > 0) {
            System.out.println(username + "卖票成功，当前票号为：" + tickets--);
        }

    }
}



